package com.example.mynetwork2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.example.mynetwork2.recyclerview.RecyclerVActivity;
import com.example.mynetwork2.recyclerview.netrequest.RecyclerVNetRequestActivity;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button button;
    private ImageView my_image;
    private Handler handler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
           switch (msg.what){
               case 0:
                   Bitmap bitmap = (Bitmap)msg.obj;
                   my_image.setImageBitmap(bitmap);
                   break;

           }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.download_bt);
        my_image = (ImageView) findViewById(R.id.my_image);
        button.setOnClickListener(this);
//         button.setOnClickListener(new View.OnClickListener() {
//             @Override
//             public void onClick(View v) {
//
//             }
//         });
//         button.setOnClickListener((v)->{
//
//         });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.download_bt) {
            downloadImage();
        }

    }

    private void downloadImage() {
//        HttpURLConnection connection = null;
//        Bitmap bitmap =null;
//        try {
//            URL url = new URL("http://pic1.win4000.com/wallpaper/c/53cdd1f7c1f21.jpg");
//            connection= (HttpURLConnection) url.openConnection();
//            int responseCode = connection.getResponseCode();
//            if (responseCode==200){
//                InputStream in = connection.getInputStream();
//                 bitmap = BitmapFactory.decodeStream(in);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//         //主线程不能执行耗时的网络请求
//        my_image.setImageBitmap(bitmap);

//        //使用异步任务去下载图片
//        MyTask myTask = new MyTask();
//        //执行
//        myTask.execute();
        //使用子线程下载图片，并利用handler更新UI
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//
//            }
//        }).start();
        new Thread(()->{
            HttpURLConnection connection = null;
            Bitmap bitmap = null;
            try {
                URL url = new URL("http://pic1.win4000.com/wallpaper/c/53cdd1f7c1f21.jpg");
                connection = (HttpURLConnection) url.openConnection();
                int responseCode = connection.getResponseCode();
                if (responseCode == 200) {
                    InputStream in = connection.getInputStream();
                    bitmap = BitmapFactory.decodeStream(in);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (bitmap!=null){
                //方式一：
//                Message msg = Message.obtain();
//                msg.what =0;
//                msg.obj = bitmap;
//                handler.sendMessage(msg);
                //方式二：
                Bitmap finalBitmap = bitmap;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        my_image.setImageBitmap(finalBitmap);
                    }
                });
            }


        }).start();
    }

    public void toOkhttp(View view) {
        Intent intent = new Intent(this, OkHttpActivity.class);
        startActivity(intent);
    }
    //跳转到列表界面（获取本地资源的界面）
    public void toListAct(View view) {
        Intent intent = new Intent(this, RecyclerVActivity.class);
        startActivity(intent);
    }

    public void toListNetAct(View view) {
        Intent intent = new Intent(this, RecyclerVNetRequestActivity.class);
        startActivity(intent);
    }


    //定义异步任务
    class MyTask extends AsyncTask<Void, Void, Bitmap> {

        @Override
        protected Bitmap doInBackground(Void... voids) {
            HttpURLConnection connection = null;
            Bitmap bitmap = null;
            try {
                URL url = new URL("http://pic1.win4000.com/wallpaper/c/53cdd1f7c1f21.jpg");
                connection = (HttpURLConnection) url.openConnection();
                int responseCode = connection.getResponseCode();
                if (responseCode == 200) {
                    InputStream in = connection.getInputStream();
                    bitmap = BitmapFactory.decodeStream(in);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            my_image.setImageBitmap(bitmap);
        }
    }
}